package com.photoeditor.demo.ui.widget;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import com.common.base.ComponentContext;
import com.common.base.utils.DeviceUtils;
import com.photoeditor.R;

/**
 * Created by Administrator on 2017/8/16 0016.
 * 贴纸参数调节seekBar
 */

public class CustomSimpleSeekBar extends View {

    private GestureDetector mGestureDetector;
    private SeekBarGestureListener mGestureListener;
    private OnSeekBarChangeListener mSeekBarChangeListener;

    private int mHeight;
    private int mWidth;

    private Paint mBgPaint;
    private Paint mBitmapPaint;

    private Bitmap mThumbBitmap;
    private RectF mThumbRect;
    private RectF mBgRect;

    //当前thumb移动的距离
    private float mOffset;
    //thumb可移动的最大距离
    private float mMaxValue;

    //最大进度
    private float mMaxProgress;

    private int progress;

    private int mThumbWidth;
    private int mBgWidth;

    private int[] colors;

    public CustomSimpleSeekBar(Context context) {
        this(context, null);
    }

    public CustomSimpleSeekBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomSimpleSeekBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mThumbBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_zoom_bar_thumb);
        mThumbWidth = DeviceUtils.dip2px(ComponentContext.getContext(), 15);
        mThumbRect = new RectF();

        mBgWidth = DeviceUtils.dip2px(ComponentContext.getContext(), 5);
        mBgRect = new RectF();

        mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBgPaint.setStyle(Paint.Style.FILL);

        mBitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBitmapPaint.setFilterBitmap(true);

        mGestureListener = new SeekBarGestureListener();
        mGestureDetector = new GestureDetector(getContext(), mGestureListener);

        mMaxProgress = 100;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
        mMaxValue = mWidth - mThumbWidth;

        mOffset = (progress / mMaxProgress) * mMaxValue;
        mThumbRect.set(mOffset, mHeight / 2 - mThumbWidth / 2, mOffset + mThumbWidth, mHeight / 2 + mThumbWidth / 2);

        mBgRect.set(mThumbWidth / 2, mHeight / 2 - mBgWidth / 2, mWidth - mThumbWidth / 2, mHeight / 2 + mBgWidth / 2);

        LinearGradient linearGradient = new LinearGradient(mThumbWidth / 2, mHeight / 2, mWidth - mThumbWidth / 2, mHeight / 2, colors,
                null, Shader.TileMode.CLAMP);
        mBgPaint.setShader(linearGradient);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //背景
        canvas.drawRoundRect(mBgRect, mBgWidth / 2, mBgWidth / 2, mBgPaint);

        mThumbRect.left = mOffset;
        mThumbRect.right = mOffset + mThumbWidth;
        canvas.drawBitmap(mThumbBitmap, null, mThumbRect, mBitmapPaint);
    }

    private void updateOffset(float offset) {
        mOffset = (int) offset;
        if (mOffset < 0)
            mOffset = 0;

        if (mOffset > mMaxValue) {
            mOffset = mMaxValue;
        }
        progress = (int) ((mOffset / mMaxValue) * mMaxProgress);
        invalidate();

        if (mSeekBarChangeListener != null) {
            mSeekBarChangeListener.onProgressChanged(this, progress);
        }
    }

    public void setProgress(int progress) {
        this.progress = progress;
        mOffset = (progress / mMaxProgress) * mMaxValue;
        if (mOffset < 0) {
            mOffset = 0;
        }
        if (mOffset > mMaxValue) {
            mOffset = mMaxValue;
        }

        invalidate();
        if (mSeekBarChangeListener != null) {
            mSeekBarChangeListener.onProgressChanged(this, progress);
        }
    }

    public void setMaxProgress(int maxProgress) {
        this.mMaxProgress = maxProgress;
    }

    public void setBackGroundColor(int[] colors) {
        this.colors = colors;
    }

    public void setSeekBarChangeListener(OnSeekBarChangeListener seekBarChangeListener) {
        mSeekBarChangeListener = seekBarChangeListener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            updateOffset(event.getX());
            if (mSeekBarChangeListener != null) {
                mSeekBarChangeListener.onStopTrackingTouch(this);
            }
            return true;
        }
        return mGestureDetector.onTouchEvent(event);
    }

    private class SeekBarGestureListener extends
            GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                                float distanceX, float distanceY) {
            updateOffset(e2.getX());
            return true;
        }

        @Override
        public boolean onDown(MotionEvent e) {
            if (mSeekBarChangeListener != null) {
                mSeekBarChangeListener.onStartTrackingTouch(CustomSimpleSeekBar.this);
            }
            return true;
        }

    }

    public interface OnSeekBarChangeListener {

        void onProgressChanged(CustomSimpleSeekBar seekBar, int progress);

        void onStartTrackingTouch(CustomSimpleSeekBar seekBar);

        void onStopTrackingTouch(CustomSimpleSeekBar seekBar);
    }
}
